var m = angular.module("mainapp",[]);

m.controller("topcontroller",function($rootScope,$scope,$location) {
	
	$rootScope.getParam = function (paramName) { 
	    paramValue = "", isFound = !1; 
	    if (window.location.search.indexOf("?") == 0 && window.location.search.indexOf("=") > 1) { 
	        arrSource = unescape(window.location.search).substring(1, window.location.search.length).split("&"), i = 0; 
	        while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++ 
	    } 
	    return paramValue == "" && (paramValue = null), paramValue 
	}
	
	$rootScope.setCookie = function(name,value){
		var cookie = [name, '=', JSON.stringify(value)].join('');
		document.cookie = cookie;
	}
 
	$rootScope.getCookie = function(name){
		var result = document.cookie.match(new RegExp(name + '=([^;]+)'));
		result && (result = JSON.parse(result[1]));
		return result;
	}
	
	$rootScope.deleteCookie = function(name) {
		  if ($scope.getCookie(name))
			  $scope.setCookie(name, "", -1);
	}

	$rootScope.getLoggedUser = function(){
		
		if($rootScope.userOfLoger == null || $rootScope.userOfLoger ==''){
			$.ajax({
	            type:"POST",
	            url:"/login/getSession",
	            contentType:"application/x-www-form-urlencoded",
	            dataType:"json",
	            success:function(data){
	                if(data.value!=null){
	                	$rootScope.userOfLoger = data.value;
	                 }else{
	                	$rootScope.userOfLoger = null;
	                }

            		 $scope.$apply();
	            }
			});
		}
		
		
		
	}
	
	
	$rootScope.htmlInit = function(){
		
		
		$rootScope.getLoggedUser();
		
	}
	$rootScope.htmlInit();
    
});

